package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.auth.CustomException;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealCheckgroupDao;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealCheckgroupService;
import com.itheima.service.SetmealService;
import org.apache.ibatis.annotations.Param;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 预约管理-套餐管理
 */

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired

    private SetmealDao setmealDao;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SetmealCheckgroupService setmealCheckgroupService;

    @Autowired
    private SetmealCheckgroupDao setmealCheckgroupDao;

    /**
     * 根据id删除套餐
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //根据id先查询order表中有无关联的数据
        int count = orderService.countBySetmealId (id);
        if (count > 0) {
            //存在
            throw new CustomException ("该套餐正在被使用，不能删除");
        }
        //不存在 可以删除
        setmealDao.deleteById (id);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal getById(Integer id) {
        Setmeal setmeal = setmealDao.getById (id);
        if (setmeal != null) {
            return setmeal;
        } else {
            throw new CustomException ("查询套餐数据失败");
        }
    }

    /**
     * 根据套餐id查询对应的检查组id集合
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> getCheckgroupById(Integer id) {
        List<Integer> ids = setmealCheckgroupService.getCheckgroupById (id);
        if (ids != null && ids.size ( ) > 0) {
            return ids;
        } else {
            throw new CustomException ("查询套餐失败");
        }
    }

    /**
     * 编辑套餐
     *
     * @param checkgroupIds 检查组id集合
     * @param setmeal       套餐数据
     */
    @Override
    public void update(List<Integer> checkgroupIds, Setmeal setmeal) {
        Integer setmealId = setmeal.getId ( );
        if (setmealId != null) {
            //根据套餐id删除所有关联的检查组
            setmealCheckgroupService.deleteBySetmealId (setmealId);
            //重新添加新的关联的检查组
            for (Integer checkgroupId : checkgroupIds) {
                SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup ( );
                setmealCheckgroup.setSetmealId (setmealId);
                setmealCheckgroup.setCheckgroupId (checkgroupId);
                setmealCheckgroupService.saveSetmealCheckgroup (setmealCheckgroup);
            }

            //修改套餐数据
            setmealDao.updateById (setmeal);
        }

    }

    //套餐分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString ( );
        Integer pageSize = queryPageBean.getPageSize ( );
        Integer currentPage = queryPageBean.getCurrentPage ( );
        PageHelper.startPage (currentPage, pageSize);
        Page<Setmeal> setmeals = setmealDao.getPage (queryString);
        return new PageResult (setmeals.getTotal ( ), setmeals.getResult ( ));
    }

    //套餐添加数据
    @Override
    public void insertSetmeal(Setmeal setmeal, List<Integer> checkgroupIds) {

       setmealDao.insertSetmeal (setmeal);
        Integer id = setmeal.getId ( );
        setmealCheckgroupDao.insertSetmealIdAndCheckGroupId (checkgroupIds,id );

    }
}

